/**
 * 
 */
package com.platform.common.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <pre>
 * 功能：一些数据结构处理类型
 * </pre>
 * 
 * @author wei.gao
 */
public class DataUtils {
	
	/**
	 * <pre>
	 * 功能：集合转Set
	 * </pre>
	 * 
	 * @param collection
	 * @return
	 */
	public static <T> Set<T> collection2Set(Collection<T> collection) {
		Set<T> result = null;
		if (collection != null && !collection.isEmpty()) {
			if (collection instanceof Set) {
				return (Set<T>) collection;
			} else {
				result = new HashSet<>();
				result.addAll(collection);
			}
		}
		return result;
	}
	
	/**
	 * <pre>
	 * 功能：获得对象列表
	 * </pre>
	 * 
	 * @param ints
	 * @return
	 */
	public static <T> List<T> array2List(T[] objs) {
		List<T> list = new ArrayList<T>();
		for (T i : objs) {
			list.add(i);
		}
		return list;
	}
	
	/**
	 * <pre>
	 * 功能：获得Integer对象列表
	 * </pre>
	 * 
	 * @param ints
	 * @return
	 */
	public static List<Integer> getList(int... ints) {
		List<Integer> list = new ArrayList<Integer>();
		for (int i : ints) {
			list.add(i);
		}
		return list;
	}
	
	/**
	 * <pre>
	 * 功能：获得反转的list
	 * </pre>
	 * 
	 * @param list
	 * @return
	 */
	public static <T> List<T> getReversionList(List<T> list) {
		Stack<T> stack = new Stack<T>();
		for (T t : list) {
			stack.push(t);
		}
		return getListFromStack(stack);
	}
	
	/**
	 * <pre>
	 * 功能：获得List stack转list
	 * </pre>
	 * 
	 * @param stack
	 * @return
	 */
	private static <T> List<T> getListFromStack(Stack<T> stack) {
		List<T> l = new ArrayList<T>();
		while (!stack.empty()) {
			l.add(stack.pop());
		}
		return l;
	}
	
	/**
	 * <pre>
	 * 功能：获得int数组
	 * </pre>
	 * 
	 * @param strings
	 * @return
	 */
	public static int[] getIntArray(String... strings) {
		if (strings == null || strings.length < 1) {
			return new int[] {};
		}
		int[] array = new int[strings.length];
		for (int i = 0; i < array.length; i++) {
			array[i] = StringUtils.getInt(strings[i]);
		}
		return array;
	}
	
	/**
	 * <pre>
	 * 功能：获得Integer数组
	 * </pre>
	 * 
	 * @param it
	 * @return
	 */
	public static Integer[] getIntegerArrayFromIterable(Iterable<?> it) {
		if (it == null) {
			return new Integer[0];
		}
		List<Integer> list = new ArrayList<Integer>();
		for (Object obj : it) {
			list.add(StringUtils.getInt(obj));
		}
		return list.toArray(new Integer[list.size()]);
	}
	
	/**
	 * <pre>
	 * 功能：获得Integer数组
	 * </pre>
	 * 
	 * @param strings
	 * @return
	 */
	public static Integer[] getIntegerArray(String... strings) {
		if (strings == null) {
			return new Integer[0];
		}
		List<Integer> list = new ArrayList<Integer>();
		for (String str : strings) {
			list.add(StringUtils.getInt(str));
		}
		return list.toArray(new Integer[list.size()]);
	}
	
	/**
	 * <pre>
	 * 功能：获取中文字数
	 * </pre>
	 * 
	 * @param word
	 * @return
	 * @throws Exception
	 */
	public final static int getWordCountForZHCN(String word) throws Exception {
		int count = 0;
		
		boolean status = StringUtils.isNotBlank(word);
		if (status) {
			final String regEx = "[\u4e00-\u9fa5]";
			Pattern pattern = Pattern.compile(regEx);
			Matcher matcher = pattern.matcher(word);
			while (matcher.find()) {
				for (int i = 0; i <= matcher.groupCount(); i++) {
					count = count + 1;
				}
			}
		}
		
		return count;
	}
	
	/**
	 * <pre>
	 * 功能：获取中文字数
	 * </pre>
	 * 
	 * @param word
	 * @param duplicate
	 * @return
	 * @throws Exception
	 */
	public final static int getWordCountForZHCN(String word, boolean duplicate) throws Exception {
		int count = 0;
		
		boolean status = StringUtils.isNotBlank(word);
		if (status) {
			if (!duplicate) {
				StringBuilder stringBuilder = new StringBuilder();
				Set<String> dataSet = new HashSet<>();
				for (int i = 0; i < word.length(); i++) {
					char data = word.charAt(i);
					String temp = String.valueOf(data);
					if (!dataSet.contains(temp)) {
						stringBuilder.append(data);
						dataSet.add(temp);
					}
				}
				word = stringBuilder.toString();
			}
			count = getWordCountForZHCN(word);
		}
		
		return count;
	}
	
}
